﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json.Linq;

namespace DataIssue.Service
{
    public class CashFlowService
    {
        /// <summary>
        /// 净流入
        /// </summary>
        /// <returns></returns>
        public List<Entity.CashFlow> GetProfitFlow()
        {
            List<Entity.CashFlow> datas = new List<Entity.CashFlow>();
            F10ParamEntity param = new F10ParamEntity("Base", "V_BS_QUICKWIN2_ZJNETIN", 2);
            param.Filters = new List<string>();
            param.Filters.Add("DAYS-int=1");
            param.Limit = 8;
            param.OrderBy = "ZJNETIN DESC";

            JToken clientData;
            int totalCount = 0;
            F10Query.TryParse(param, false, out clientData, out totalCount);
            if (clientData != null && totalCount > 0)
            {
                for (int i = 0; i < clientData.Count(); i++)
                {
                    double temp = 0f;
                    Entity.CashFlow data = new Entity.CashFlow();
                    data.Profit = clientData[i].J_Double("ZJNETIN", temp);
                    data.Precent = clientData[i].J_Double("ZJNETINPCT", temp);
                    data.Name = clientData[i].J_String("STOCKNAME");
                    data.Code = clientData[i].J_String("STOCKCODE");
                    datas.Add(data);
                }
            }
            return datas;
        }

        /// <summary>
        /// 连续流入
        /// </summary>
        /// <returns></returns>
        public List<Entity.CashFlow> GetContinueFlow()
        {
            List<Entity.CashFlow> datas = new List<Entity.CashFlow>();
            F10ParamEntity param = new F10ParamEntity("Base", "V_BS_QUICKWIN2_ZJCONTNETIN", 2);
            //param.Filters = new List<string>();
            //param.Filters.Add("NETINDAYS-int=1");
            param.Limit = 8;
            param.OrderBy = "NETINDAYS DESC";

            JToken clientData;
            int totalCount = 0;
            F10Query.TryParse(param, false, out clientData, out totalCount);
            if (clientData != null && totalCount > 0)
            {
                for (int i = 0; i < clientData.Count(); i++)
                {
                    double temp = 0f;
                    Entity.CashFlow data = new Entity.CashFlow();
                    data.Profit = clientData[i].J_Double("ZJNETIN", temp);
                    data.Precent = clientData[i].J_Double("ZJNETINPCT", temp);
                    data.Name = clientData[i].J_String("STOCKNAME");
                    data.Code = clientData[i].J_String("STOCKCODE");
                    data.Days = clientData[i].J_Integer("NETINDAYS", 0);
                    datas.Add(data);
                }
            }
            return datas;
        }

        /// <summary>
        /// 行业资金
        /// </summary>
        /// <returns></returns>
        public List<Entity.CashFlow> GetIndurstryFlow()
        {
            List<Entity.CashFlow> datas = new List<Entity.CashFlow>();
            F10ParamEntity param = new F10ParamEntity("Base", "V_BS_QUICKWIN2_INDUSTRYLX", 2);
            param.Filters = new List<string>();
            param.Filters.Add("BKCODE-ne-str=C9");
            param.Limit = 8;
            param.OrderBy = "CURRNETIN DESC";

            JToken clientData;
            int totalCount = 0;
            F10Query.TryParse(param, false, out clientData, out totalCount);
            if (clientData != null && totalCount > 0)
            {
                for (int i = 0; i < clientData.Count(); i++)
                {
                    double temp = 0f;
                    Entity.CashFlow data = new Entity.CashFlow();
                    data.Profit = clientData[i].J_Double("CURRNETIN", temp);
                    data.Precent = clientData[i].J_Double("CURRNETINPCT", temp);
                    data.Name = clientData[i].J_String("BKNAME");
                    data.Code = clientData[i].J_String("BKCODE");
                    datas.Add(data);
                }
            }
            return datas;
        }
    }
}